package com.ice.base.controller;

import com.ice.base.model.SysMenu;
import com.ice.base.model.SysPermissionModel;
import com.ice.base.model.SysRoleModel;
import com.ice.base.service.SysMenuService;
import com.ice.base.service.SysPermissionService;
import com.ice.base.service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@ApiIgnore(value = "系统后台-综合管理")
public class BaseApiController extends BaseController {

    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysPermissionService sysPermissionService;



    @ApiOperation(value = "菜单-初始化")
    @PostMapping("/base/RBAC/All")
    public List selectAllRolePermission() {
        ArrayList<Object> resultDataList = new ArrayList<>();
        try{
            List<SysRoleModel> sysRoleModels = sysRoleService.selectListDetails(new SysRoleModel());

            if (sysRoleModels != null) {
                getRolesTree(resultDataList, sysRoleModels);
                System.out.println();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultDataList;
    }

    @ApiOperation(value = "菜单管理-初始化")
    @PostMapping("/base/RBAC/permissions")
    public List selectPermission() {
        ArrayList<Object> resultDataList = new ArrayList<>();
        try{
            //查询所有的权限
            ArrayList<SysPermissionModel> sysPermissionModels = sysPermissionService.selectAll();
            //查询所有的菜单
            ArrayList<SysMenu> sysMenuModels = sysMenuService.selectAll();

            if (sysPermissionModels != null) {
                createRBACPermission(sysMenuModels, resultDataList, sysPermissionModels);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultDataList;
    }


    private void createRBACPermission(ArrayList<SysMenu> sysMenuModels, ArrayList<Object> resultDataList, List<SysPermissionModel> Permissions) {
        for (SysPermissionModel permissionModel : Permissions) {
            HashMap<String, Object> dataMap = new HashMap<>();
            if (sysMenuModels != null) {
                for (SysMenu sysMenuModel : sysMenuModels) {
                    if (sysMenuModel.getId().equals(permissionModel.getPermission_band_menu_id())) {
                        dataMap.put("id", permissionModel.getId());
                        dataMap.put("name", permissionModel.getPermission_name());

                        dataMap.put("tid", sysMenuModel.getId());
                        if ("无".equals(sysMenuModel.getMenu_parent_id())) {
                            dataMap.put("isParent", true);
                        } else
                            dataMap.put("tpid", sysMenuModel.getMenu_parent_id());

                        resultDataList.add(dataMap);
                        break;
                    }
                }
            }
        }
    }


    private void getRolesTree(ArrayList<Object> resultDataList, List<SysRoleModel> roles) {
        for (SysRoleModel roleModel : roles) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("id", roleModel.getId());
            stringObjectHashMap.put("name", roleModel.getRole_name());
            List<SysPermissionModel> permissions = roleModel.getPermissions();
            if (permissions != null) {
                for (SysPermissionModel permission : permissions) {
                    HashMap<String, Object> chileMenuMap = new HashMap<>();
                    chileMenuMap.put("id", permission.getId());
                    chileMenuMap.put("name", permission.getPermission_name());
                    chileMenuMap.put("pId", roleModel.getId());
                    chileMenuMap.put("nocheck", true);
                    resultDataList.add(chileMenuMap);
                }
            }
            resultDataList.add(stringObjectHashMap);
        }
    }

    //--------------------------RBAC-------------------------------




    //---------------------------登陆角色-----------------------start

/*
    @ApiOperation(value = "登陆角色-插入")
    @PostMapping("/base/SysLoginRole/insert.do")
    public HashMap insert_SysLoginRole(HttpServletRequest request) {
        return insert(request, sysLoginRoleService, new SysLoginRoleModel());
    }

    @ApiOperation(value = "登陆角色-删除")
    @PostMapping("/base/SysLoginRole/deleteById.do")
    public HashMap deleteById_SysLoginRole(@RequestParam("id") String id) {
        return delete(sysLoginRoleService, id);
    }

    @ApiOperation(value = "登陆角色-更新")
    @PostMapping("/base/SysLoginRole/updateById.do")
    public HashMap update_SysLoginRole(HttpServletRequest request) {
        return update(request, sysLoginRoleService, new SysLoginRoleModel());
    }

    @ApiOperation(value = "登陆角色-查询一个")
    @PostMapping("/base/SysLoginRole/selectById.do")
    public HashMap selectById_SysLoginRole(@RequestParam("id") String id) {
        return selectById(sysLoginRoleService, id);
    }

    @ApiOperation(value = "登陆角色-查询多个")
    @PostMapping("/base/SysLoginRole/selectList.do")
    public HashMap selectList_SysLoginRole(HttpServletRequest request) {
        return selectListByCondition(request, sysLoginRoleService, new SysLoginRoleModel());
    }

    @ApiOperation(value = "登陆角色-查询分页")
    @PostMapping("/base/SysLoginRole/selectListByPageHelper.do")
    public HashMap selectListByPageHelper_SysLoginRole(HttpServletRequest request) {
        return selectListByPageHelper(request, sysLoginRoleService, new SysLoginRoleModel());
    }


    //---------------------------角色权限-----------------------start

    @ApiOperation(value = "角色权限-插入")
    @PostMapping("/base/SysRolePermission/insert.do")
    public HashMap insert_SysRolePermission(HttpServletRequest request) {
        return insert(request, sysRolePermissionService, new SysRolePermissionModel());
    }

    @ApiOperation(value = "角色权限-删除")
    @PostMapping("/base/SysRolePermission/deleteById.do")
    public HashMap deleteById_SysRolePermission(@RequestParam("id") String id) {
        return delete(sysRolePermissionService, id);
    }

    @ApiOperation(value = "角色权限-更新")
    @PostMapping("/base/SysRolePermission/updateById.do")
    public HashMap update_SysRolePermission(HttpServletRequest request) {
        return update(request, sysRolePermissionService, new SysRolePermissionModel());
    }

    @ApiOperation(value = "角色权限-查询一个")
    @PostMapping("/base/SysRolePermission/selectById.do")
    public HashMap selectById_SysRolePermission(@RequestParam("id") String id) {
        return selectById(sysRolePermissionService, id);
    }

    @ApiOperation(value = "角色权限-查询多个")
    @PostMapping("/base/SysRolePermission/selectList.do")
    public HashMap selectList_SysRolePermission(HttpServletRequest request) {
        return selectListByCondition(request, sysRolePermissionService, new SysRolePermissionModel());
    }

    @ApiOperation(value = "角色权限-查询分页")
    @PostMapping("/base/SysRolePermission/selectListByPageHelper.do")
    public HashMap selectListByPageHelper_SysRolePermission(HttpServletRequest request) {
        return selectListByPageHelper(request, sysRolePermissionService, new SysRolePermissionModel());
    }

    //---------------------------角色权限-----------------------end


    //---------------------------操作日志-----------------------start
    @Resource
    private SysOperationLogService sysOperationLogService;

    @ApiIgnore(value = "操作日志-插入")
    @PostMapping("/base/SysOperationLog/insert.do")
    public HashMap insert_SysOperationLog(HttpServletRequest request) {
        return insert(request, sysOperationLogService, new SysOperationLogModel());
    }

    @ApiIgnore(value = "操作日志-删除")
    @PostMapping("/base/SysOperationLog/deleteById.do")
    public HashMap deleteById_SysOperationLog(@RequestParam("id") String id) {
        return delete(sysOperationLogService, id);
    }

    @ApiIgnore(value = "操作日志-更新")
    @PostMapping("/base/SysOperationLog/updateById.do")
    public HashMap update_SysOperationLog(HttpServletRequest request) {
        return update(request, sysOperationLogService, new SysOperationLogModel());
    }

    @ApiIgnore(value = "操作日志-查询一个")
    @PostMapping("/base/SysOperationLog/selectById.do")
    public HashMap selectById_SysOperationLog(@RequestParam("id") String id) {
        return selectById(sysOperationLogService, id);
    }

    @ApiIgnore(value = "操作日志-查询多个")
    @PostMapping("/base/SysOperationLog/selectAll.do")
    public HashMap selectList_SysOperationLog(HttpServletRequest request) {
        return selectListByCondition(request, sysOperationLogService, new SysOperationLogModel());
    }

    @ApiIgnore(value = "操作日志-查询分页")
    @PostMapping("/base/SysOperationLog/selectListByPageHelper.do")
    public HashMap selectListByPageHelper_SysOperationLog(HttpServletRequest request) {
        return selectListByPageHelper(request, sysOperationLogService, new SysOperationLogModel());
    }

    //---------------------------操作日志-----------------------end*/

}
